package com.atguigu.daijia.common.feign;

import feign.codec.Decoder;
import feign.optionals.OptionalDecoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.HttpMessageConverterCustomizer;
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @create: 2024/9/21
 * @author: gyhe10
 * @description: OpenFeign 配置类
 **/
@Configuration
public class FeignConfig {

    /**
     * 自定义解码器
     *      创建解码器：通过组合多个解码器实现，包括 SpringDecoder 和 FeignCustomDataDecoder
     *          SpringDecoder：Spring MVC 的解码器，用于将响应数据转换为 Java 对象。
     *          FeignCustomDataDecoder：自定义解码器，用于处理自定义的解码逻辑，如处理自定义的响应数据。
     *      参数注入：接受消息转换器工厂 msgConverters 和消息转换器定制器提供者 customizers 作为参数
     *      返回自定义解码器：最终返回一个 OptionalDecoder 实例，用于处理 HTTP 响应解码
     *
     * @param msgConverters
     * @param customizers
     * @return
     */
    @Bean
    public Decoder decoder(ObjectFactory<HttpMessageConverters> msgConverters, ObjectProvider<HttpMessageConverterCustomizer> customizers) {
        return new OptionalDecoder((new ResponseEntityDecoder(new FeignCustomDataDecoder(new SpringDecoder(msgConverters, customizers)))));
    }
}
